<?php
/**
 * 购物车
 */
namespace V3\Controller;

use Think\Controller;

class CartController extends ApiCommonController
{

    //添加商品到购物车
    public function add()
    {
        $data['product_id'] = trim(I('post.product_id'));
		$data['product_count'] = trim(I('post.product_count'));
        $data['product_color'] = trim(I('post.product_color'));
        $data['product_guige'] = trim(I('post.product_guige'));
		$data['user_id'] = trim(I('post.user_id'));
        if (!in_array('',$data)){
        	$where['product_id']=$data['product_id'];
        	$gdata = M('Goods')->where('id='.$data['product_id'])->find();
        	$where['user_id']=$data['user_id'];			
			$where['product_color']=  $data['product_color'];
			$where['product_guige']= $data['product_guige'];
			$where['jxs_id']=$gdata['movie_id'];
			$cart=M('Cart')->where($where)->find();
			
			
			
			if(!$cart){
				
				  $data['product_brand'] = $gdata['brand'];
                $data['product_name'] = $gdata['name'];
                $data['product_title'] = $gdata['title'];
                $data['product_desc'] = $gdata['base_info'];
                $data['product_img'] = $gdata['square_photo'];
                $data['product_price'] = $gdata['price'];              
                $data['jxs_id'] = $gdata['movie_id'];              
                $name = M('Movie_v3')->getFieldById($data['jxs_id'],'name');
               	$data['jxs_title'] = $name ;				
                $data['created'] = time();
                $data['modified'] = time();
                $re = M('Cart')->add($data);             
                if ($re) {
                	$count=M('Cart')->where('user_id='.$data['user_id'])->count();       			
                    $return_data['status'] = '1';
                    $return_data['info'] = '成功!';
					$return_data['data']=$count;
                } else {
                    $return_data['status'] = '2';
                    $return_data['info'] = '失败!';
                }			
				
						
			} else{         
            	$count=M('Cart')->where('user_id='.$data['user_id'])->count();    
          		$product_price['product_price']=$gdata['price'];			
				M('Cart')->where($where)->setInc('product_count',$data['product_count']);
				M('Cart')->where($where)->save($product_price);	  	
            	 $return_data['status'] = '1';
                 $return_data['info'] = '成功!';
					$return_data['data']=$count;
              
            }
		}else{
			 $return_data['status'] = '2';
             $return_data['info'] = '参数错误！';
			
			
		}
        $this->ajaxReturn($return_data);

    }

    //移除购物车商品
    public function del(){
       	$id =  trim(I('post.id'));
		if(empty($id)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}
		$where['id']=array('in',$id);
		$re = M('Cart')->where($where)->delete();   
		if ($re) {
	 		$return_data['status'] = '1';
			$return_data['info'] = '成功!';
		}else {
	 		$return_data['status'] = '2';
			$return_data['info'] = '失败!';
		}  
        $this->ajaxReturn($return_data);
    }


      //修改购物车内商品数量
    public function editnum(){

		
		$id = trim(I('post.id'));
		$alg = trim(I('post.alg'));
		 
		if(empty($id)||empty($alg)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}
		$cart = M('Cart')->where(array('id'=>$id))->find();
		if(empty($cart)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '参数提交有误',
			));
		}
		if($alg=='add'){  //增加购买数量
			$product = M('Goods')->where(array('id'=>$cart['product_id']))->find();
			//当购买数量大于库存数量时
			if($cart['product_count']+1>$product['number']){
				$return_data = array(
					'status'=>'2',
					'info' => '超出商品库存，最多能购买'.$product['number'].'件',
					'data' => $product['number']
				);
			}else{
				//增加购买数量
				$rs = M('Cart')->where(array('id'=>$id))->setInc('product_count');
				if($rs){
					$return_data = array(
						'status'=>'1',
						'info' => '更新完成',
						'data' => $cart['product_count']+1
					);
				}else{
					$return_data = array(
						'status'=>'2',
						'info' => '更新失败，请重试',
						'data' => $product['number']
					);
				}
			}
		}elseif($alg=='sub'){  //减少购买数量
			//购买数量不能小于1件
			if($cart['product_count']==1){
				$return_data = array(
					'status'=>'2',
					'info' => '购买数量不能小于1件',
					'data' => $product['number']
				);
			}
			//减少购买数量
			$rs = M('Cart')->where(array('id'=>$id))->setDec('product_count');
				if($rs){
					$return_data = array(
						'status'=>'1',
						'info' => '更新完成',
						'data' => $cart['product_count']-1
					);
				}else{
					$return_data = array(
						'status'=>'2',
						'info' => '更新失败，请重试',
						'data' => $product['number']
					);
				}
		}else{
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '参数提交有误',
			));
		}
		$this->ajaxReturn($return_data);
		
		}

    //获取购物车
     public function getcart() 
    {
        $user_id = trim(I('get.user_id'));
		
        if ($user_id) {
            $where['user_id']=$user_id;
            $cartData = M('Cart')->where($where)->select();
			
			$tempData = M('Cart')->where($where)->group('jxs_title')->field('jxs_title')->select();
			$cartData_real=array();
				foreach($tempData as $t_key =>$t_val){
					$temp=array();
					$temp['name']=$t_val['jxs_title'];
					$temp['image']=M('Movie_v3')->getFieldByName($t_val['jxs_title'],'roundness_photo');
					foreach($cartData as $key=>$val) {
						if ($val['jxs_title'] == $t_val['jxs_title']) {
							$temp['data'][] = $val;
						}
					}
					$cartData_real[]=$temp;
				}
			
            if ($cartData) {
                $return_data['status'] = '1';
                $return_data['info'] = '成功!';
                $return_data['data'] = $cartData_real;
            } else {
                $return_data['status'] = '1';
                $return_data['info'] = '结果为空!';
                $return_data['data'] = array();
            }
        } else {
            $return_data['status'] = '2';
            $return_data['info'] = '参数不足!';
        }
        $this->ajaxReturn($return_data);

	}

}
